package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.DishInfoMapper;
import org.example.dish_pojo.DishInfo;
import org.example.dish_pojo.DishSpec;
import org.example.dish_pojo.Result;
import org.example.service.IDishSpecService;
import org.example.vo.SpecQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜品规格及价格差异表 前端控制器
 * </p>
 *
 * @author mp
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/dish-spec")
public class DishSpecController {

    @Autowired
    private IDishSpecService dishSpecService;
    @Autowired
    private DishInfoMapper dishInfoMapper;


    // 添加菜品规格
    @RequestMapping("/addDishSpec")
    public Result addDishSpec(@RequestBody DishSpec dishSpec) {
        boolean save = dishSpecService.save(dishSpec);
        if (!save) {
            return new Result(500, "添加失败", null);
        } else {
            return new Result(200, "添加成功", null);
        }
    }


    // 删除菜品规格
    @RequestMapping("/deleteDishSpec")
    public Result deleteDishSpec(Integer specId) {
        boolean delete = dishSpecService.removeById(specId);
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }


    // 批量删除菜品规格
    @RequestMapping(value = "/deleteDishSpecs", method = RequestMethod.POST)
    public Result deleteDishSpecs(@RequestParam("specIds") Integer[] specIds) {
        boolean delete = dishSpecService.removeByIds(Arrays.asList(specIds));
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }


    // 根据id回显数据
    @RequestMapping("/getDishSpecById")
    public Result getDishSpecById(@RequestParam("specId") Integer specId) {
        DishSpec dishSpec = dishSpecService.getById(specId);
        return new Result(200, "操作成功", dishSpec);
    }


    // 修改菜品规格
    @RequestMapping("/updateDishSpec")
    public Result updateDishSpec(@RequestBody DishSpec dishSpec) {
        boolean update = dishSpecService.updateById(dishSpec);
        if (!update) {
            return new Result(500, "修改失败", null);
        }
        return new Result(200, "修改成功", null);
    }


    // 分页查询数据
    @RequestMapping("/listDishSpec")
    public Result listDishSpec(@RequestBody SpecQuery querySpec) {
        Page<DishSpec> page = new Page<>(querySpec.getCurrent(), querySpec.getSize());
        QueryWrapper<DishSpec> wrapper = new QueryWrapper<>();
        wrapper.like(querySpec.getSpecName() != null, "spec_name", querySpec.getSpecName());
        dishSpecService.page(page, wrapper);
        List<DishSpec> list = page.getRecords();
        for (DishSpec d : list){
            Integer dishId = d.getDishId();
            DishInfo dishInfo = dishInfoMapper.selectById(dishId);
            d.setDname(dishInfo.getName());
        }
        return new Result(200, "操作成功", page);
    }


}

